package com.jeeplus.test.website.infoPublish.controller;


import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.test.website.infoPublish.domain.vo.InfoPublishReqVo;
import com.jeeplus.test.website.infoPublish.domain.vo.InfoPublishRespVo;
import com.jeeplus.test.website.infoPublish.service.InfoPublishService;
import com.jeeplus.test.tools.resp.R;
import com.jeeplus.test.tools.resp.ZyPage;
import com.jeeplus.test.tools.resp.ZyPageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@Api(tags = "网站管理-资讯发布")
@RestController
@Slf4j
@RequestMapping("/infoPublish")
public class InfoPublishController {

    @Autowired
    private InfoPublishService infoPublishService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public R<ZyPage<InfoPublishRespVo>> getInfoPublishPage(InfoPublishReqVo infoPublishReqVo, ZyPageRequest pageRequest) {
        long current = pageRequest.getPageNo();
        long size = pageRequest.getPageSize();
        Page<InfoPublishRespVo> pageResult = infoPublishService.getInfoPublishPage(infoPublishReqVo, current, size);
        List<InfoPublishRespVo> dataList = pageResult.getRecords();
        long total = pageResult.getTotal();
        ZyPage<InfoPublishRespVo> retPage = ZyPage.getPage(dataList, pageRequest, total);
        return R.suc(retPage);
    }

    @ApiOperation("通过主键查询")
    @GetMapping("/queryById/{id}")
    public R<InfoPublishRespVo> queryById(@PathVariable("id") String id) {
        InfoPublishRespVo archivesInfoNwRespVO = infoPublishService.queryById(id);
        return R.suc(archivesInfoNwRespVO);
    }

    @ApiOperation("新增")
    @PostMapping("/add")
    public R addInfoPublish(InfoPublishReqVo infoPublishReqVo) {
        return BooleanUtil.isTrue(infoPublishService.addInfoPublish(infoPublishReqVo)) ? R.suc() : R.error();
    }

    @ApiOperation("修改")
    @GetMapping("/update")
    public R updateInfoPublish(InfoPublishReqVo infoPublishReqVo) throws ParseException {
        return BooleanUtil.isTrue(infoPublishService.updateInfoPublish(infoPublishReqVo)) ? R.suc() : R.error();
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    public R deleteInfoPublish(@PathVariable("id") String id) {
        return BooleanUtil.isTrue(infoPublishService.deleteInfoPublish(id)) ? R.suc() : R.error();
    }

}
